package cn.com.taiji.core;

import cn.hutool.core.util.StrUtil;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.activerecord.generator.MetaBuilder;
import com.jfinal.plugin.activerecord.generator.TableMeta;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author dongwei dongweia@mail.taiji.com.cn
 */

/**
 * 重写代码生成核心方法
 */
public class CommonMetaBuilder extends MetaBuilder {
    private static String[] tableNames;
    private String prefix;

    public CommonMetaBuilder(DataSource dataSource, String[] tableNames, String prefix) {
        super(dataSource);
        super.setDialect(new OracleDialect());
        this.tableNames = tableNames;
        this.prefix = prefix;
    }

    @Override
    public List<TableMeta> build() {
        System.out.println("CommonMetaBuilder TableMeta ...");

        try {
            this.conn = this.dataSource.getConnection();
            this.dbMeta = this.conn.getMetaData();
            List<TableMeta> ret = new ArrayList<>();
            this.buildTableNames(ret);
            setTableName(ret, tableNames, this.prefix);

            Iterator var2 = ret.iterator();

            while (var2.hasNext()) {
                TableMeta tableMeta = (TableMeta) var2.next();
                this.buildPrimaryKey(tableMeta);
                this.buildColumnMetas(tableMeta);
            }

            List var13 = ret;
            if (null == var13 || var13.size() < 1) {
                throw new RuntimeException("当前表不存在," + tableNames);
            }
            return var13;
        } catch (SQLException var11) {
            throw new RuntimeException(var11);
        } finally {
            if (this.conn != null) {
                try {
                    this.conn.close();
                } catch (SQLException var10) {
                    throw new RuntimeException(var10);
                }
            }

        }
    }


    private final void setTableName(List<TableMeta> list, String[] tableNames, String sufferix) {

        if (null == tableNames || tableNames.length < 1) {
            throw new RuntimeException("tablenames不能空");
        } else {
            List<TableMeta> newTableMetaList = new ArrayList<>();

            for (TableMeta tableMeta : list) {
                if (isExist(tableNames, tableMeta)) {
                    if (StrUtil.isNotBlank(sufferix)) {
                        tableMeta.baseModelName += sufferix;
                    }
                    newTableMetaList.add(tableMeta);
                }
            }
            list.clear();
            list.addAll(newTableMetaList);
        }
    }

    private boolean isExist(String[] tableNames, TableMeta tableMeta) {
        for (String tableName : tableNames) {
           /* System.out.println(tableName.toUpperCase() + "---" + tableMeta.name.toUpperCase());
            if (StrUtil.equalsIgnoreCase(tableName.toUpperCase(), tableMeta.name.toUpperCase())) {
                return true;
            }*/
            if (tableName.toUpperCase().equalsIgnoreCase(tableMeta.name.toUpperCase())) {
                return true;
            }
        }
        return false;
    }
}
